package org.zjut.db;

import com.alibaba.fastjson.JSON;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.zjut.util.CommonUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @date 2017-08-13 21:04
 * @author Rising
 * @des 属性相关操作
 */
public class AttributeDataBase extends JdbcDaoSupport {
    String dataBase = "pso_recommond.";
    String tb_attr = "attribute";

    //=========================属性相关操作=========================*/

    /**
     * 新增属性
     * @param params
     * @return
     */
    public Map<String,Object> addAttribute(Map<String,Object> params){
        Map<String,Object> result = new HashMap<>();
        String name,type,limitL,limitU;
        try{
            name = params.get("attrName").toString();
            type = params.get("attrType").toString();
            limitL = params.get("limitL").toString();
            limitU = params.get("limitU").toString();
        }catch (Exception e){
            System.out.println(e.getMessage()+"");
            return CommonUtil.setErrorInfo("请求参数有问题");
        }
        System.out.println(JSON.toJSONString(params));
        String sql = "INSERT INTO "+dataBase+tb_attr+" (code,name,type,limitL,limitU) VALUE(?,?,?,?,?) ";
        try{
            String code = UUID.randomUUID().toString();
            getJdbcTemplate().update(sql,new Object[]{code,name,type,limitL,limitU});
            Map<String,Object> message = new HashMap<>();
            message.put("attrCode",code);
            result.put("success",true);
            result.put("message",message);
        }catch (Exception e){
            System.out.println(e.getMessage()+"");
            return CommonUtil.setErrorInfo("新增失败");
        }
        return result;
    }

    /**
     * 编辑属性
     * @param params
     * @return
     */
    public Map<String,Object> editAttribute(Map<String,Object> params){
        Map<String,Object> result = new HashMap<>();
        String code,name,type,limitL,limitU;
        try{
            code = params.get("attrCode").toString();
            name = params.get("attrName").toString();
            type = params.get("attrType").toString();
            limitL = params.get("limitL").toString();
            limitU = params.get("limitU").toString();
        }catch (Exception e){
            System.out.println(e.getMessage()+"");
            return CommonUtil.setErrorInfo("请求参数有问题");
        }

        String sql = "UPDATE "+dataBase+tb_attr+" SET name = ? ,type =? ,limitL = ? , limitU = ? " +
                " WHERE code = ?" ;
        try{
            getJdbcTemplate().update(sql,new Object[]{name,type,limitL,limitU,code});
            result.put("success",true);
            result.put("message","修改成功");
        }catch (Exception e){
            System.out.println(e.getMessage()+"");
            return CommonUtil.setErrorInfo("修改失败");
        }
        return result;
    }

    /**
     * 获取属性
     * @param params
     * @return
     */
    public Map<String,Object> getAtrribute(Map<String,Object> params){
        Map<String,Object> result = new HashMap<>();
        String attrCode = null,attrName= null ,attrType = null;
        try{
            if(params.containsKey("attrCode")){
                attrCode = params.get("attrCode").toString();
            }
            if(params.containsKey("attrName")){
                attrName = params.get("attrName").toString();
            }
            if(params.containsKey("attrType")){
                attrType = params.get("attrType").toString();
            }
        }catch (Exception e){
            System.out.println(e.getMessage()+"");
            return CommonUtil.setErrorInfo("请求参数有问题");
        }
        //查找学习资源属性
        String sql = "SELECT a.code as attrCode , a.name as attrName, a.type as attrType, a.limitL as limitL , a.limitU as limitU  FROM "+dataBase+tb_attr+" as a " +
                "WHERE 1=1 ";
        if(attrCode!=null){
            sql = sql + " AND a.code = '"+attrCode+"' ";
        }
        if(attrName!=null){
            sql = sql + " AND a.anme = '"+attrName +"' ";
        }
        if(attrType!=null){
            sql = sql + " AND a.type = '"+attrType+" ' ";
        }
        try{
            List<Map<String,Object>> attr = getJdbcTemplate().queryForList(sql);
            Map<String,Object> message = new HashMap<>();
            message.put("attribute",attr);
            result.put("success",true);
            result.put("message",message);
        }catch (Exception e){
            System.out.println(e.getMessage()+"");
            return CommonUtil.setErrorInfo("查询失败");
        }
        return result;
    }

    /**
     * 删除属性
     * @param attrCode
     * @return
     */
    public Map<String,Object> delAttribute(String attrCode){
        Map<String,Object> result = new HashMap<>();
        String sql  = "DELETE FROM "+dataBase+tb_attr+" WHERE code = '"+attrCode+"' ";
        try{
            getJdbcTemplate().update(sql);
            result.put("success",true);
            result.put("message","删除成功");
        }catch (Exception e){
            System.out.println(e.getMessage()+"");
            return CommonUtil.setErrorInfo("删除失败");
        }
        return result;
    }
}
